import java.util.Iterator;

class MyQueue<Item> implements Iterable<Item> {
    private Node<Item> first;
    private Node<Item> last;
    private int N = 0;


    private static class Node<Item> {
        private Item item;
        private Node<Item> next;
    }

    public int size() {
        return N;
    }

    public void enqueue(Item item) {
        Node<Item> oldLast = last;
        last = new Node<Item>();
        last.item = item;
        last.next = null;
        if (isEmpty()) {
            first = last;
        } else {
            oldLast.next = last;
        }
        N++;
    }

    public Item dequeue() {
        Item item = first.item;
        first = first.next;
        if (isEmpty()) {
            last = null;
        }
        N--;
        return item;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public Item deleteLast() {
        Node<Item> current = first;
        if (isEmpty()) {
            return null;
        } else if (current.next == null) {
            first = null;
            return current.item;
        }
        while (current.next.next != null) {
            current = current.next;
        }
        Item lastItem = current.next.item;
        current.next = null;
        last = current;
        return lastItem;
    }

    public Item deleteByIndex(int k) {
        if (isEmpty()) {
            return null;
        }
        if (k > N) {
            return null;
        } if (k == 1) {
            return dequeue();
        }

        Node<Item> current = first;
        Node<Item> previous = null;

        for (int i = 0; i < k; i++) {
            previous = current;
            current = current.next;
        }

        if (previous != null) {
            previous.next = current.next;
        }

        return current.item;
    }

    public Iterator<Item> iterator() {
        return new QueueIterator() {
        };
    }

    private class QueueIterator implements Iterator<Item> {
        private Node<Item> current = first;

        public boolean hasNext() {
            return current != null;
        }

        public Item next() {
            Item item = current.item;
            current = first.next;
            return item;
        }
    }

    public static void main(String[] args) {
        MyQueue<String> q = new MyQueue<>();

        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            if (item.equals("=")) {
                StdOut.print(q.deleteLast() + " ");
            } else if (!item.equals("-")) {
                q.enqueue(item);
            } else if (!q.isEmpty()) {
                StdOut.print(q.dequeue() + " ");
            }
        }


        StdOut.println("(" + q.size() + " left on queue)");
    }
}
